package com.jtjrenren.android.taxi.passenger.core.user.register;

import android.os.CountDownTimer;

import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.User;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiResultCommonStr;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;

import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.HttpParseUtils;
import com.library.mvp.presenter.BasePresenter;
import com.library.utils.RegexUtils;
import com.library.utils.StringUtils;

/**
 * Created by Administrator on 2016/9/13.
 */

public class RegisterPresenterImpl extends BasePresenter<RegisterView, RegisterInteractor> implements RegisterPresenter {
    CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            view.refreshCountDown((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            view.downFinish();
        }
    };

    private String phone, pwd;

    public RegisterPresenterImpl(RegisterView view) {
        super(view);
    }

    @Override
    public void init() {

    }

    @Override
    public void destory() {
        if (countDownTimer != null)
            countDownTimer.cancel();
        countDownTimer = null;
    }

    @Override
    public void sendYzm(final String phone) {
        if (StringUtils.isEmpty(phone) && !RegexUtils.isPhoneNo(phone)) {
            view.showTip("请输入正确的手机号");
            return;
        }
        this.phone = phone;
        interactor.checkPhoneUni(phone, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
            @Override
            public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1) {
                    countDownTimer.start();
                    view.startCountDown();
                    interactor.sendCode(phone, new ApiWorks.ResponseListener() {
                        @Override
                        public void onResponse(Object o) {

                        }
                    });
                } else {
                    view.showTip("手机号已经注册过了");
                }

            }
        });
    }

    @Override
    public void register(final String phone, String code, final String pwd) {
        if (!RegexUtils.isPhoneNo(phone)) {
            view.showTip(AppManger.getInstance().getString(R.string.pleaseinputphoneright));
            return;
        }
        if (code == null || "".equals(code)) {
            view.showTip(AppManger.getInstance().getString(R.string.pleaseinputcode));
            return;
        }
        if (pwd == null || "".equals(pwd)) {
            view.showTip(AppManger.getInstance().getString(R.string.pleaseinputpwdright));
            return;
        }

        if (pwd.length() < 6 || pwd.length() > 20) {
            view.showTip(AppManger.getInstance().getString(R.string.mimachagnduxianzhi));
            return;
        }
        this.phone = phone;
        this.pwd = pwd;

        interactor.checkCode(phone, code, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
            @Override
            public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1)
                    interactor.register(phone, pwd, new ApiWorks.ResponseListener<String>() {
                        @Override
                        public void onResponse(String s) {
                            User user = null;
                            if (!StringUtils.isEmpty(s)) {
                                String[] resuts = HttpParseUtils.parseUserCommonResult(s);
                                if (resuts.length == 3) {
                                    user = new User();
                                    user.setWorkNo(resuts[0]);
                                }
                            }

                            if (user != null) {
                                view.showTip("注册成功");
                                view.finishOk(true);
                            } else {
                                view.showTip("注册失败");
                            }
                        }
                    });
                else
                    view.showTip("验证码错误");
            }
        });
    }


    @Override
    public RegisterInteractor createInteractor() {
        return new RegisterInteractorImpl();
    }
}
